<template>
  <card one="文章管理" two="文章列表">
    <template #button>
      <el-button type="primary" plain @click="addArt">发表文章</el-button>
    </template>
    <template #main>
      <el-form
        :inline="true"
        :model="q"
        class="demo-form-inline"
        ref="selFrom"
        size="small"
      >
        <el-form-item label="文章分类">
          <el-select v-model="q.cate_id" placeholder="请选择分类">
            <el-option
              v-for="item in artList"
              :key="item.id"
              :label="item.cate_name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布状态">
          <el-select v-model="q.state" placeholder="请选择状态">
            <el-option label="已发布" value="已发布"></el-option>
            <el-option label="草稿" value="草稿"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">筛选</el-button>
          <el-button type="info" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="list" style="width: 100%" stripe border>
        <el-table-column prop="title" label="文章标题" width="180">
          <template #default="{ row }">
            <el-link type="primary" @click="lookArt(row.id)">{{
              row.title
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="cate_name" label="分类" width="180">
        </el-table-column>
        <el-table-column label="发表时间">
          <template v-slot="{ row }">
            {{ row.pub_date | format }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="success" size="mini">编辑</el-button>
            <el-button type="danger" size="mini" @click="delArt(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="q.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="q.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-dialog title="文章预览" :visible.sync="dialogVisible" width="70%">
        <art-info :userInfo="userInfo"></art-info>
        <span slot="footer" class="dialog-footer"> </span>
      </el-dialog>
      <el-dialog
        title="发表文章"
        :visible.sync="dialogVisibleAdd"
        :before-close="beforeClose"
        destroy-on-close
        fullscreen
      >
        <art-pub @setVal="pubLishClose"></art-pub>
      </el-dialog>
    </template>
  </card>
</template>

<script>
import { getArtList, getLookArtList, delOldArt } from '@/api/art'
import { mapState } from 'vuex'
import ArtInfo from './components/ArtInfo.vue'
import ArtPub from './components/ArtPub.vue'
export default {
  name: 'artList',
  components: { ArtInfo, ArtPub },
  data () {
    return {
      q: {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
      },
      list: [],
      total: 0,
      dialogVisible: false,
      dialogVisibleAdd: false,
      userInfo: {}
    }
  },
  computed: {
    ...mapState('article', ['artList'])
  },
  created () {
    this.getList()
  },
  methods: {
    onSubmit () {
      this.getList()
    },
    onReset () {
      this.q.cate_id = this.q.state = ''
      this.getList()
    },
    async getList () {
      const { data } = await getArtList(this.q)
      if (data.code !== 0) return this.$message.error(data.message)
      this.list = data.data
      this.total = data.total
    },
    handleSizeChange (val) {
      this.q.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.q.pagenum = val
      this.getList()
    },
    async lookArt (id) {
      const { data } = await getLookArtList(id)
      if (data.code !== 0) return this.$message.error(data.message)
      this.userInfo = data.data
      this.dialogVisible = true
    },
    addArt () {
      this.dialogVisibleAdd = true
    },
    async beforeClose () {
      const res = await this.$confirm(
        '此操作将导致文章信息丢失, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (res === 'confirm') {
        this.dialogVisibleAdd = false
      } else {
        return this.$message.info('已取消')
      }
    },
    pubLishClose () {
      this.dialogVisibleAdd = false
      this.getList()
    },
    async delArt (id) {
      const res = await this.$confirm(
        '此操作将删除文章信息, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (res !== 'confirm') return this.$message.info('已取消')
      const { data } = await delOldArt(id)
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.success(data.message)
      if (this.q.pagenum > 1 && this.list.length === 1) {
        // 则删除成功之后，当前页的数据条数为 0，需要请求上一页数据
        this.q.pagenum--
      }
      this.getList()
    }
    // eidtArt () {
    // @click="eidtArt(row.id)"
    //   this.dialogVisibleAdd = true
    // }
  }
}
</script>
<style lang='less'  scoped>
</style>
